<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="字典分类编码">
        <el-input v-model="form.typeCode" disabled />
      </el-form-item>
      <el-form-item label="字典名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入字典名称" />
      </el-form-item>
      <el-form-item label="字典值" prop="value">
        <el-input v-model="form.value" placeholder="请输入字典值" />
      </el-form-item>
      <el-form-item label="颜色" prop="dictColor">
        <el-input
          v-model="form.dictColor"
          type="color"
          placeholder="请输入颜色"
        />
      </el-form-item>
      <el-form-item label="仅开发编辑" prop="exploit">
        <el-select
          v-model="form.exploit"
          placeholder="请选择菜单类型"
          clearable
        >
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.sort" placeholder="请输入排序值" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { addDict, editDict } from '@/api/dictionaryManagement'
  export default defineComponent({
    name: 'DictionaryManagementEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        form: {
          id: null,
          typeCode: '',
          value: '',
          name: '',
        },
        typeList: [
          {
            label: '是',
            value: '1',
          },
          {
            label: '否',
            value: '0',
          },
        ],
        rules: {
          value: [{ required: true, trigger: 'blur', message: '请输入字典值' }],
          name: [
            { required: true, trigger: 'blur', message: '请输入字典名称' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      })
      const showEdit = (row) => {
        state.form = JSON.parse(JSON.stringify(row))
        if (row.id) {
          state.title = '编辑字典'
        } else {
          state.title = '添加字典'
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        state.form = {
          id: null,
          value: '',
          name: '',
          typeCode: '',
        }
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            try {
              console.log(state.form, 'ddsd')
              const { id, name, value, typeCode, dictColor, sort, exploit } =
                state.form
              const params = {
                id,
                name,
                value,
                typeCode,
                dictColor,
                sort,
                exploit,
              }
              if (id) {
                await editDict(params)
              } else {
                await addDict(params)
              }
              $baseMessage('操作成功', 'success', 'vab-hey-message-success')
              emit('fetch-data')
              close()
            } catch (e) {
              console.error(e)
            }
          }
        })
      }

      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
      }
    },
  })
</script>
